<template>
  <div class="block">
    <div class="filter-container">
      <el-date-picker v-model="filterDate" type="daterange" range-separator="至" start-placeholder="开始日期"
        end-placeholder="结束日期" @change="filterTimeline">
      </el-date-picker>
    </div>
    <el-timeline>
      <el-timeline-item v-for="(item, index) of timeline" :key="index" :timestamp="item.timestamp" placement="top">
        <el-card>
          <h4>{{ item.title }}</h4>
          <p>{{ item.content }}</p>
        </el-card>
      </el-timeline-item>
    </el-timeline>
  </div>
</template>

<script>
export default {
  data() {
    return {
      filterDate: null,
      originalTimeline: [], // 保存原始数据
      timeline: [
        {
          timestamp: '2019/4/20',
          title: 'Update Github template',
          content: 'PanJiaChen committed 2019/4/20 20:46'
        },
        {
          timestamp: '2019/4/21',
          title: 'Update Github template',
          content: 'PanJiaChen committed 2019/4/21 20:46'
        },
        {
          timestamp: '2019/4/22',
          title: 'Build Template',
          content: 'PanJiaChen committed 2019/4/22 20:46'
        },
        {
          timestamp: '2019/4/23',
          title: 'Release New Version',
          content: 'PanJiaChen committed 2019/4/23 20:46'
        }
      ]
    }
  },
  created() {
    this.originalTimeline = [...this.timeline]
  },
  methods: {
    filterTimeline() {
      if (!this.filterDate) {
        this.timeline = [...this.originalTimeline]
        return
      }
      const [start, end] = this.filterDate
      this.timeline = this.originalTimeline.filter(item => {
        const date = new Date(item.timestamp)
        return date >= start && date <= end
      })
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
  align-self: flex-end;
}
</style>